﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UIShare
{
    public class MoneyConvertor
    {
        private static string[] DigitsSign = new string[] { "", "万", "亿", "兆" };

        private string test()
        {
            return ConvertToChinese(0.3344m);
        }

        public static string ConvertToChinese(decimal money)
        {
            if (money < 0)
                money = 0 - money;

            decimal fraction = money - Math.Floor(money);
            StringBuilder sb = new StringBuilder();
            int integer = Convert.ToInt32(Math.Floor(money));
            if (integer == 0)
                sb.Append("零元");
            else
            {
                int[] digits = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    integer = Math.DivRem(integer, 10000, out digits[i]);
                }

                for (int i = 3; i >= 0; i--)
                {
                    int temp = digits[i];

                    int thousands = temp / 1000;
                    temp = temp - thousands * 1000;
                    if (thousands != 0)
                        sb.Append(string.Format("{0}仟", ConvertNumericToChinese(thousands)));

                    int hundred = temp / 100;
                    temp = temp - hundred * 100;
                    if (hundred != 0)
                        sb.Append(string.Format("{0}佰", ConvertNumericToChinese(hundred)));

                    int ten = temp / 10;
                    temp = temp - ten * 10;
                    if (ten != 0)
                        sb.Append(string.Format("{0}拾", ConvertNumericToChinese(ten)));

                    int pc = temp;
                    if (pc != 0)
                        sb.Append(ConvertNumericToChinese(pc));

                    if (i == 0)
                        sb.Append("元");
                    else if (digits[i] != 0)
                        sb.Append(DigitsSign[i]);
                }
            }

            int horn = Convert.ToInt32(Math.Floor(fraction * 10));
            if (horn != 0)
                sb.Append(string.Format("{0}角", ConvertNumericToChinese(horn)));
            fraction = fraction * 10 - horn;

            int cent = Convert.ToInt32(Math.Floor(fraction * 10));
            if (cent != 0)
                sb.Append(string.Format("{0}分", ConvertNumericToChinese(cent)));

            if (horn == 0 && cent == 0)
                sb.Append("整");

            return sb.ToString();
        }

        private static string ConvertNumericToChinese(int numeric)
        {
            switch (numeric)
            {
                case 0:
                    return "零";
                case 1:
                    return "壹";
                case 2:
                    return "贰";
                case 3:
                    return "叁";
                case 4:
                    return "肆";
                case 5:
                    return "伍";
                case 6:
                    return "陆";
                case 7:
                    return "柒";
                case 8:
                    return "捌";
                case 9:
                    return "玖";
                default:
                    throw new Exception("转换出错");
            }
        }
    }
}
